package com.proposition.support.filter;

import org.springframework.util.StringUtils;
import org.springframework.web.util.HtmlUtils;
import org.springframework.web.util.JavaScriptUtils;

import java.beans.PropertyEditorSupport;

/**
 * 输入过滤
 * <p/>
 *
 * @author Administrator
 */
public class InputFilterEditor extends PropertyEditorSupport {

    private boolean escapeHTML;
    private boolean escapeJavaScript;
    private boolean escapeSQL;

    public InputFilterEditor() {
        super();
    }

    public InputFilterEditor(boolean escapeHTML, boolean escapeJavaScript, boolean escapeSQL) {
        super();
        this.escapeHTML = escapeHTML;
        this.escapeJavaScript = escapeJavaScript;
        this.escapeSQL = escapeSQL;
    }

    @Override
    public void setAsText(String text) throws IllegalArgumentException {
        if (!StringUtils.hasText(text)) {
            setValue(null);
        } else {
            String value = text;
            if (escapeJavaScript) {
                value = JavaScriptUtils.javaScriptEscape(text);
            }
            if (escapeHTML) {
                value = HtmlUtils.htmlEscape(text);
            }
            if (escapeSQL) {
                // value = SQlu
            }
            super.setValue(value.trim());
        }
    }
}